package com.zsyh.jiujiu.Adress;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.DizhiBean;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.net.MyObserver;

import java.util.ArrayList;
import java.util.List;

import chihane.jdaddressselector.AddressProvider;
import chihane.jdaddressselector.model.Province;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by zzq on 16/9/26.
 */
public class CityFragment implements AdapterView.OnItemClickListener {
    private AddressCallBack callBack;
    private String code;
    private Activity context;
    private AddressAdapter adapter;
    private ListView listview;
    private String provinceCode;
    private List<AddressManager.City> cityList = new ArrayList<AddressManager.City>();






    public CityFragment(Activity context, AddressCallBack callBack) {
        this.context = context;
        this.callBack = callBack;
        initView();
    }

    public void setCode(String provinceCode, String code) {
        Log.e("provinceCodebbs", provinceCode + "kon");
      /*  if (!provinceCode.equals(this.provinceCode)) {
            this.code = null;
        }*/
        if (StringUtils.isNoEmpty(code)) {
            //this.code = code;
        }
        this.provinceCode = provinceCode;
        cityList.clear();
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }
        sheng(provinceCode);



    }

    public ListView getListview() {
        return listview;
    }

    public View initView() {
        listview = (ListView) LayoutInflater.from(context).inflate(R.layout.select_address_pop_listview, null);
        listview.setOnItemClickListener(this);
        return listview;
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        code = cityList.get(i).getCode();
        if (callBack != null) {
            callBack.selectCity(cityList.get(i));
        }
        adapter.notifyDataSetChanged();
    }

    class AddressAdapter extends BaseAdapter {

        private List<AddressManager.City> list;

        public AddressAdapter(List<AddressManager.City> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            if (list != null) {
                return list.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(context).inflate(R.layout.address_listiew_item_textview, null);
            TextView text = (TextView) view.findViewById(R.id.tvTextName);
            ImageView ivSelect = (ImageView) view.findViewById(R.id.ivSelect);
            text.setText(list.get(i).getName());
            if (list.get(i).getCode().equals(code)) {
                text.setTextColor(context.getResources().getColor(R.color.new_redbg));
                ivSelect.setVisibility(View.VISIBLE);
            }
            return view;
        }
    }






    /**
     * 获取省份
     */
    public void sheng(String id){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getAreaByPid(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(context,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("dizhi",JsonStr);
                        Gson gson=new Gson();
                        DizhiBean dizhiBean=gson.fromJson(JsonStr,DizhiBean.class);
                        for(int i=0;i<dizhiBean.getData().size();i++){
                            AddressManager.City city = new AddressManager.City(dizhiBean.getData().get(i).getAreaName(), dizhiBean.getData().get(i).getId(), id);
                            cityList.add(city);
                        }
                        adapter = new AddressAdapter(cityList);
                        listview.setAdapter(adapter);


                    }
                });
    }

}
